/*
========================================================================
 Name        : CallForeverSettingItemListView.cpp
 Author      : jimmy54
 Copyright   : welcome to my weibo:www.weibo.com/jimmy54
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <akncontext.h>
#include <akntitle.h>
#include <stringloader.h>
#include <barsread.h>
#include <eikbtgpc.h>
#include <CallForever.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]

#include "CallForever.hrh"
#include "CallForeverSettingItemListView.h"
#include "CallForeverSettingItemList.hrh"
#include "CallForeverSettingItemList.h"
#include "../inc/CallsMonitor.h"
#include "../inc/SimulateEvent.h"
#include "../inc/Vibrate.h"
#include "../inc/DialCall.h"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CCallForeverSettingItemListView::CCallForeverSettingItemListView():
iIsDialCall(ETrue),
iMonitor(NULL),
iDialCall(NULL),
iTimer(NULL)
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CCallForeverSettingItemListView::~CCallForeverSettingItemListView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]

	if (iMonitor)
		{
		delete iMonitor;
		iMonitor = NULL;
		}
	
	if (iDialCall)
		{
		delete iDialCall;
		iDialCall = NULL;
		}
	
	if (iTimer)
		{
		delete iTimer;
		iTimer = NULL;
		}
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CCallForeverSettingItemListView
 */
CCallForeverSettingItemListView* CCallForeverSettingItemListView::NewL()
	{
	CCallForeverSettingItemListView* self = CCallForeverSettingItemListView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CCallForeverSettingItemListView
 */
CCallForeverSettingItemListView* CCallForeverSettingItemListView::NewLC()
	{
	CCallForeverSettingItemListView* self = new ( ELeave ) CCallForeverSettingItemListView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CCallForeverSettingItemListView::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Code]
	BaseConstructL( R_CALL_FOREVER_SETTING_ITEM_LIST_CALL_FOREVER_SETTING_ITEM_LIST_VIEW );
				
	// ]]] end generated region [Generated Code]
	
	// add your own initialization code here
	iMonitor = CCallMonitor::NewL(*this);
	iDialCall = CDialCall::NewL();
	iTimer = CPeriodic::New(CActive::EPriorityStandard);
	
	}

/**
 * @return The UID for this view
 */
TUid CCallForeverSettingItemListView::Id() const
	{
	return TUid::Uid( ECallForeverSettingItemListViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CCallForeverSettingItemListView::HandleCommandL( TInt aCommand )
	{
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{	// code to dispatch to the AknView's menu and CBA commands is generated here
		case ECallForeverSettingItemListViewMenuItem1Command:
			commandHandled = HandleChangeSelectedSettingItemL( aCommand );
			break;
		case ECallForeverSettingItemListViewMenuDialCommand:
			{
			commandHandled = HandleChangeSelectedSettingDialL( aCommand );
			}
		default:
			break;
		}
	
		
	if ( !commandHandled ) 
		{
	
		if ( aCommand == EAknSoftkeyExit )
			{
			AppUi()->HandleCommandL( EEikCmdExit );
			}
	
		}
	// ]]] end generated region [Generated Code]
	
	}

/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CCallForeverSettingItemListView::DoActivateL( 
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	SetupStatusPaneL();
	
				
	if ( iCallForeverSettingItemList == NULL )
		{
		iSettings = TCallForeverSettingItemListSettings::NewL();
		iCallForeverSettingItemList = new ( ELeave ) CCallForeverSettingItemList( *iSettings, this );
		iCallForeverSettingItemList->SetMopParent( this );
		iCallForeverSettingItemList->ConstructFromResourceL( R_CALL_FOREVER_SETTING_ITEM_LIST_CALL_FOREVER_SETTING_ITEM_LIST );
		iCallForeverSettingItemList->ActivateL();
		iCallForeverSettingItemList->LoadSettingValuesL();
		iCallForeverSettingItemList->LoadSettingsL();
		AppUi()->AddToStackL( *this, iCallForeverSettingItemList );
		} 
	// ]]] end generated region [Generated Contents]
	
	}

/**
 */
void CCallForeverSettingItemListView::DoDeactivate()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CleanupStatusPane();
	
	if ( iCallForeverSettingItemList != NULL )
		{
		AppUi()->RemoveFromStack( iCallForeverSettingItemList );
		delete iCallForeverSettingItemList;
		iCallForeverSettingItemList = NULL;
		delete iSettings;
		iSettings = NULL;
		}
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CCallForeverSettingItemListView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
		{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
		}
	
	// [[[ begin generated region: do not modify [Generated Code]
	// ]]] end generated region [Generated Code]
	
	}

// [[[ begin generated function: do not modify
void CCallForeverSettingItemListView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_FOREVER_SETTING_ITEM_LIST_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	}

// ]]] end generated function

// [[[ begin generated function: do not modify
void CCallForeverSettingItemListView::CleanupStatusPane()
	{
	}

// ]]] end generated function

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CCallForeverSettingItemListView::HandleChangeSelectedSettingItemL( TInt aCommand )
	{
	iCallForeverSettingItemList->ChangeSelectedItemL();
	return ETrue;
	}

TBool CCallForeverSettingItemListView::HandleChangeSelectedSettingDialL(TInt aCommand )
	{
	DialCall();
	return ETrue;
	}

void CCallForeverSettingItemListView::DialCall()
	{
	TDes& no = iSettings->No();
	if (no.Length() > 0)
		{
		iDialCall->DialNewCall(no);
		iIsDialCall = ETrue;
		}
	else
		{
		//notify
		}
	}
	

void CCallForeverSettingItemListView::StopDial()
	{
	iDialCall->Cancel();
	iIsDialCall = EFalse;
	iTimer->Cancel();
	}

void CCallForeverSettingItemListView::StartTimer()
	{
	iTimer->Start(100, 100, TCallBack(Start, this));
	}

TInt CCallForeverSettingItemListView::Start(TAny* aPtr)
	{
	CCallForeverSettingItemListView* view = (CCallForeverSettingItemListView*)aPtr;
	view->DialCall();
	return KErrNone;
	}

void CCallForeverSettingItemListView::CallStatusChangedL(CTelephony::TCallStatus& aStatus, TInt aError)
	{
	switch (aStatus)
		{
		case CTelephony::EStatusUnknown:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusUnknown"));
			}
			break;
		case CTelephony::EStatusIdle:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusIdle"));
			}
			break;
		case CTelephony::EStatusDialling:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusDialling"));
			}
			break;
		case CTelephony::EStatusRinging:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusRinging"));
			}
			break;
		case CTelephony::EStatusAnswering:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusAnswering"));
			}
			break;
		case CTelephony::EStatusConnecting:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusConnecting"));
			}
			break;
		case CTelephony::EStatusConnected:
			{
			if (iSettings->Type() == 0)
				{
				Vibrate::DoVibrateL(3000);
				}
			else if (iSettings->Type() == 1)
				{
				SimulateEvent::SimulateRightKeyEvent();
				}
			CEikonEnv::Static()->InfoMsg(_L("EStatusConnected"));
			}
			break;
		case CTelephony::EStatusReconnectPending:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusReconnectPending"));
			}
			break;
		case CTelephony::EStatusDisconnecting:
			{
			TInt& count = iSettings->DialCount();
			if (iIsDialCall && count > 0 )
				{
				count--;
				iIsDialCall = EFalse;
//				DialCall();
				StartTimer();
				}
			CEikonEnv::Static()->InfoMsg(_L("EStatusDisconnecting"));
			}
			break;
		case CTelephony::EStatusHold:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusHold"));
			}
			break;
		case CTelephony::EStatusTransferring:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusTransferring"));
			}
			break;
		case CTelephony::EStatusTransferAlerting:
			{
			CEikonEnv::Static()->InfoMsg(_L("EStatusTransferAlerting"));
			}
			break;
		break;
		default:
		break;
		}
	}

